//
//  EditAndAddDelViewController.m
//  Utils
//
//  Created by enghou on 2018/6/5.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "EditAndAddDelViewController.h"
#import "NetWorkerManager.h"

@interface EditAndAddDelViewController ()

@end

@implementation EditAndAddDelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.ids length] == 0) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(addRecord)];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    }else{
        UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delRecord)];
        trash.tintColor = [UIColor whiteColor];
        UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(updateRecord)];
        edit.tintColor = [UIColor whiteColor];
        self.navigationItem.rightBarButtonItems = @[trash,edit];
        [self loadData];
    }
    // Do any additional setup after loading the view.
}

-(void)loadData{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.detailPath parameter:self.detailParam completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            if (self.configView) {
                self.configView(source);
            }
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSString *)addPath{
    return nil;
}

-(NSDictionary *)addedParameter{
    return nil;
}

-(void)addRecord{
    NSAssert([self.addPath length] > 0, @"请检查self.addPath字段");
    if (!self.addedParameter) {
        return;
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.addPath parameter:self.addedParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:kNetWorkFail];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSString *)updatePath{
    return nil;
}

-(NSDictionary *)updateParameter{
    return nil;
}

-(void)updateRecord{
    NSAssert([self.updatePath length] > 0, @"请检查self.updatePath字段");
    if (!self.updateParameter) {
        return;
    }
    [self beginLoading];
    [[NetWorkerManager sharedInstance]postWithPath:self.updatePath parameter:self.updateParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:kNetWorkFail];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(NSString *)delPath{
    return nil;
}

-(NSDictionary *)delParameter{
    return nil;
}

-(void)delRecord{
    NSAssert([self.delPath length] > 0, @"请检查self.delPath字段");
    if (!self.delParameter) {
        return;
    }
    [[NetWorkerManager sharedInstance]postWithPath:self.delPath parameter:self.delParameter completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            [[NSNotificationCenter defaultCenter]postNotificationName:self.refresh_not_name object:nil];
            [self pop];
        }else{
            [self alertSome:kNetWorkFail];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}
/*if (__builtin_available(<#{i, mac, tv, watch}#>OS <#x.y.z#>, *)) {
 <#API available statements#>
 } else {
 <#fallback statements#>
 }
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
